import store from "@/store";
import jsonpath from "jsonpath";
import ywuMixin from "./ywuMixin"
import {doCreatedInit} from "@/utils/componentCreatedInit";
import {sleep} from "@/utils/commonParamUtil";

/**
 * 创建tag标签
 * @param route
 * @param store
 */
export function createTag(route, store) {
    store.commit('addHeaderRouteTags', route);
}

/**
 * 对象深拷贝
 * @param obj
 * @returns {{}|Date|*}
 */
export function deepCopy(obj) {
    if (obj === null || typeof obj !== 'object') {
        return obj; // 基本类型，直接返回
    }

    if (obj instanceof Date) {
        return new Date(obj.getTime()); // 日期对象的特殊处理
    }

    if (obj instanceof Array) {
        return obj.reduce((arr, item, i) => {
            arr[i] = deepCopy(item);
            return arr;
        }, []);
    }

    if (obj instanceof Object) {
        return Object.keys(obj).reduce((newObj, key) => {
            newObj[key] = deepCopy(obj[key]);
            return newObj;
        }, {});
    }
}

/**
 * 注册组件
 * @param componentInfo
 * @returns {string|{type: ObjectConstructor, required: boolean, default: any}|Record<string, any>|*}
 */
export function registerComponent(componentInfo) {
    let componentRes = store.state.app.component(componentInfo.componentName)
    if (!componentRes) {
        let dataJson = ""
        let createdJson = ""
        if (componentInfo.componentData) {
            dataJson = JSON.parse(componentInfo.componentData)
        }
        if (componentInfo.componentCreated) {
            createdJson = JSON.parse(componentInfo.componentCreated)
        }
        // 注册组件
        // 构建组件对象
        let componentObj = {
            template: componentInfo.componentTemplate,
            data() {
                return dataJson
            },
            computed: componentInfo.componentComputed,
            mixins: [],
            created() {
                doCreatedInit(this.commonParam, createdJson)
            }
        }
        if (dataJson.isMixIn) {
            componentObj.mixins = [ywuMixin]
        }
        store.state.app.component(componentInfo.componentName, componentObj);
    }
}

export function setJsonValue(jsonData, jsonPath, newValue) {
    // 是否能够解析表达式
    let temp = jsonpath.query(jsonData, jsonPath)
    if (temp.length !== 0) {
        temp = newValue
        return
    }
    let jsonPathList = jsonPath.split(".")
    // 从做到右生成json对象
    for (let i = 1; i < jsonPathList.length; i++) {
        let tempJsonPath = getParentJsonPath(jsonPath, i)
        let currentJsonPath = getParentJsonPath(jsonPath, i + 1)
        let currentSplit = currentJsonPath.split(".")
        // 解析判断是否能够解析出来
        let currentRes = jsonpath.query(jsonData, currentJsonPath)
        if (currentRes.length === 0) {
            let tempRes = jsonpath.query(jsonData, tempJsonPath)
            tempRes.forEach(res => {
                res[currentSplit[currentSplit.length - 1]] = {}
            })
        }
    }
    // 将最后一个入参重新设置为newValue
    let lastParam = getParentJsonPath(jsonPath, jsonPathList.length - 1)
    let lastParent = jsonpath.query(jsonData, lastParam)
    lastParent.forEach(res => {
        res[jsonPathList[jsonPathList.length - 1]] = newValue
    })
}

export function getParentJsonPath(jsonPath, index) {
    let res = ""
    let jsonPathList = jsonPath.split(".")
    for (let i = 0; i < index; i++) {
        res += jsonPathList[i] + "."
    }
    return res.substring(0, res.length - 1)
}